package com.none.web.controller.logs;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;








import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TLogs;
import com.none.web.service.logs.ILogsService;

/**
 * 日志管理Controller
 * 
 * @author Summer
 * 
 */
@Controller
@RequestMapping("log")
public class LogsController {

	@Resource(name = "logService")
	ILogsService logService;

	@Autowired
	private HttpServletRequest request;
	/**
	 * 添加日志
	 * 
	 * @param log
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveLog")
	public Object saveLogs(@FormModel("log") final TLogs log, String type,String userId) throws Exception {
		String[] filter = new String[] {};

		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}

		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);

		data.put("logObj", logService.saveLogs(log, type, userId));
		return JSONUtil.toJSON(result, filter);
	}
	/**
	 * 统计浏览数量
	 * 
	 * @param contentId
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("getBrowseTimes")
	public Object getBrowseTimes(String contentId,String type,String userId) throws Exception {

		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		
		Map<String, Object> data = new HashMap<String, Object>();
		Integer times=logService.getBrowseTimes(contentId, type, userId);
		data.put("times", times);
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);
		return JSONUtil.toJSON(result);
	}
}
